package com.xaicode.learn.java.forkjoin;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Locker cjxia@isoftstone.com
 * @version 1.0
 */
public class FutureTaskDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        SonTask task1 = new SonTask("Thread Son1");
        FutureTask<String> f1 = new FutureTask<>(task1);
        new Thread(f1).start();
        FutureTask<Integer> f2 = new FutureTask<>(new MyRun(), 22);
        new Thread(f2).start();
        System.out.println("result_" + f2.get());
    }

    static class SonTask implements Callable<String> {
        private String name;

        SonTask(String name) {
            this.name = name;
        }

        @Override
        public String call() throws Exception {
            Thread.sleep(1000L);
            System.out.println(name + " done");
            return null;
        }
    }

    static class MyRun implements Runnable {
        @Override
        public void run() {
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("thread 2 is done");
        }
    }

}
